//
//  YQArticleListCell.m
//  iyuanqi
//
//  Created by czl on 2017/7/28.
//  Copyright © 2017年 czl. All rights reserved.
//

#import "YQArticleListCell.h"

@interface YQArticleListCell()

@property(nonatomic,strong)UIImageView* imageAvatar;
@property(nonatomic,strong)UILabel* labelName;
@property(nonatomic,strong)UILabel* labelTitle;
@property(nonatomic,strong)UILabel* labelTime;
@property(nonatomic,strong)NSMutableArray* arrayImages;
@property(nonatomic,strong)UIButton* btnZan;
@property(nonatomic,strong)UIButton* btnReply;


@end

@implementation YQArticleListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.imageAvatar];
        [self.imageAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(40).priorityHigh();
            make.left.top.mas_equalTo(10);
        }];
        
        [self.contentView addSubview:self.labelName];
        [self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.imageAvatar);
            make.left.mas_equalTo(self.imageAvatar.mas_right).offset(10);
        }];
        
        [self.contentView addSubview:self.labelTitle];
        [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.imageAvatar.mas_bottom).offset(10);
            make.left.mas_equalTo(self.imageAvatar);
            make.right.mas_equalTo(-10).priority(99);
        }];
        
        self.arrayImages = [NSMutableArray new];
        for (int i=0; i<3; i++) {
            UIImageView* imgV = [[UIImageView alloc]init];
            imgV.contentMode = UIViewContentModeScaleAspectFill;
            imgV.clipsToBounds = YES;
            [self.contentView addSubview:imgV];
            [self.arrayImages addObject:imgV];
        }

        [self.arrayImages mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:8 leadSpacing:10 tailSpacing:10];
        [self.arrayImages mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.labelTitle.mas_bottom).offset(10);
            make.height.mas_equalTo(100);
        }];

        [self.contentView addSubview:self.labelTime];
        [self.labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.arrayImages.count > 0) {
                UIImageView* imgV = self.arrayImages[0];
                make.top.mas_equalTo(imgV.mas_bottom).offset(10);
            } else {
                make.top.mas_equalTo(self.labelTitle.mas_bottom).offset(10);
            }
            make.left.mas_equalTo(self.imageAvatar);
            make.bottom.mas_equalTo(-10);
        }];

        [self.contentView addSubview:self.btnReply];
        [self.btnReply mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.labelTime);
            make.right.mas_equalTo(-10);
        }];

        [self.contentView addSubview:self.btnZan];
        [self.btnZan mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.labelTime);
            make.right.mas_equalTo(self.btnReply.mas_left).offset(-10);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)setArticleObject:(YQArticleObject *)articleObject {
    _articleObject = articleObject;
    [self.imageAvatar sd_setImageWithURL:[NSURL URLWithString:[Image_url stringByAppendingString:articleObject.head_img]] placeholderImage:[UIImage imageNamed:@"img_default_head"]];
    self.labelName.text = articleObject.author;
    self.labelTitle.text = articleObject.subject;
    self.labelTime.text = compareCurrentTime(getIntervalTimeByInterval(articleObject.dateline));
    [self.btnZan setTitle:[NSString stringWithFormat:@"%zd", articleObject.recommend_add] forState:UIControlStateNormal];
    [self.btnReply setTitle:[NSString stringWithFormat:@"%zd", articleObject.replies] forState:UIControlStateNormal];
    if (articleObject.images.count > 0 && articleObject.images.count <= 3) {
        for (int i=0; i<articleObject.images.count; i++) {
            UIImageView* imgV = self.arrayImages[i];
            [imgV sd_setImageWithURL:[NSURL URLWithString:articleObject.images[i]] placeholderImage:[UIImage imageNamed:@"loadimage"]];
        }
        [self.arrayImages mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(100);
        }];
    } else {
        [self.arrayImages mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}

- (UIImageView*)imageAvatar {
    if (!_imageAvatar) {
        _imageAvatar = [[UIImageView alloc]init];
        _imageAvatar.layer.cornerRadius = 20;
        _imageAvatar.layer.masksToBounds = YES;
    }
    return _imageAvatar;
}

- (UILabel*)labelName {
    if (!_labelName) {
        _labelName = [UILabel new];
    }
    return _labelName;
}

- (UILabel*)labelTitle {
    if (!_labelTitle) {
        _labelTitle = [UILabel new];
        _labelTitle.font = DefaultFont;
        _labelTitle.numberOfLines = 0;
    }
    return _labelTitle;
}

- (UILabel*)labelTime {
    if (!_labelTime) {
        _labelTime = [UILabel new];
        _labelTime.font = DefaultFont;
        _labelTime.textColor = UIColorFromRGB(0x999999);
    }
    return _labelTime;
}

- (UIButton*)btnZan {
    if (!_btnZan) {
        _btnZan = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnZan setImage:[UIImage imageNamed:@"ic_good"] forState:UIControlStateNormal];
        [_btnZan setTitle:@"0" forState:UIControlStateNormal];
        _btnZan.titleLabel.font = DefaultFont;
        [_btnZan setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    return _btnZan;
}

- (UIButton*)btnReply {
    if (!_btnReply) {
        _btnReply = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnReply setImage:[UIImage imageNamed:@"ic_comment"] forState:UIControlStateNormal];
        [_btnReply setTitle:@"0" forState:UIControlStateNormal];
        _btnReply.titleLabel.font = DefaultFont;
        [_btnReply setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    return _btnReply;
}

@end
